package com.ruoyi.common.utils;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Description: PageImpl content内容重写 覆盖类
 */
public class PageImplUtil<T> implements Iterable<T>, Serializable {
    private static final long serialVersionUID = -3720998571176536865L;
    private List<T> content = new ArrayList<>();
    private long totalElements;
    private int number;
    private int pageSize;
    private boolean first;
    private boolean last;
    private boolean empty;
    private int totalPages;
    private int numberOfElements;

    public PageImplUtil() {

    }
    //只用把原来的page类放进来即可
    public PageImplUtil(Page<T> page) {
        this.content = page.getContent();
        this.totalElements = page.getTotalElements();
        this.number = page.getPageable().getPageNumber();
        this.pageSize = page.getPageable().getPageSize();
        this.first = page.isFirst();
        this.last = page.isLast();
        this.empty = page.isEmpty();
        this.totalPages = page.getTotalPages();
        this.numberOfElements = page.getNumberOfElements();
    }

    //只用把原来的page类放进来即可
    public PageImplUtil(PageImpl<T> page) {
        this.content = page.getContent();
        this.totalElements = page.getTotalElements();
        this.number = page.getPageable().getPageNumber();
        this.pageSize = page.getPageable().getPageSize();
        this.first = page.isFirst();
        this.last = page.isLast();
        this.empty = page.isEmpty();
        this.totalPages = page.getTotalPages();
        this.numberOfElements = page.getNumberOfElements();
    }

    public List<T> getContent() {
        return content;
    }

    public void setContent(List<T> content) {
        this.content = content;
    }

    public long getTotalElements() {
        return totalElements;
    }

    public void setTotalElements(long totalElements) {
        this.totalElements = totalElements;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public boolean isFirst() {
        return first;
    }

    public void setFirst(boolean first) {
        this.first = first;
    }

    public boolean isLast() {
        return last;
    }

    public void setLast(boolean last) {
        this.last = last;
    }

    public boolean isEmpty() {
        return empty;
    }

    public void setEmpty(boolean empty) {
        this.empty = empty;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public int getNumberOfElements() {
        return numberOfElements;
    }

    public void setNumberOfElements(int numberOfElements) {
        this.numberOfElements = numberOfElements;
    }

    @Override
    public Iterator<T> iterator() {
        return getContent().iterator();
    }
}
